<template>
<j-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen @ok="handleOk" @cancel="handleCancel" cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <a-form :form="form">
            <a-row>
                <a-col :span="12">
                    <a-form-item label="货品编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['itemCode']" placeholder="请输入货品编码"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="货品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['itemName']" placeholder="请输入货品名称"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['specifications']" placeholder="请输入规格"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-decorator="['unit']" placeholder="请输入单位"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input-number :min="1" v-model="dataList.number" placeholder="请输入数量" style="width: 100%" />
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="单价" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="dataList.unitPrice" placeholder="请输入单价"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="合计" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input readOnly v-model="total" placeholder="请输入合计"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="产品批号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['productBatchNumber']" placeholder="请输入产品批号"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['note']" placeholder="请输入备注"></a-input>
                    </a-form-item>
                </a-col>
                <a-col :span="12">
                    <a-form-item label="附项" :labelCol="labelCol" :wrapperCol="wrapperCol">
                        <a-input v-decorator="['attachedItems']" placeholder="请输入附项"></a-input>
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
    </a-spin>
</j-modal>
</template>

<script>
import {
    httpAction
} from '@/api/manage'
import pick from 'lodash.pick'

export default {
    name: "SalesBillingSubModal",
    components: {},
    props: {
        mainId: {
            type: String,
            required: false,
            default: ''
        }
    },
    data() {
        return {
            form: this.$form.createForm(this),
            title: "操作",
            width: 800,
            visible: false,
            dataList: {},
            model: {},
            labelCol: {
                sm: {
                    span: 6
                },
            },
            wrapperCol: {
                sm: {
                    span: 16
                },
            },

            confirmLoading: false,
            validatorRules: {},
            url: {
                add: "/sales/salesBilling/addSalesBillingSub",
                edit: "/sales/salesBilling/editSalesBillingSub",
            }

        }
    },
    created() {},
    computed: {
        total() {
            if (this.dataList.unitPrice && this.dataList.number) {
                return (this.dataList.unitPrice * this.dataList.number) + ''
            } else {
                return ''
            }
        }
    },
    methods: {
        add() {
            this.edit({});
        },
        edit(record) {
            this.form.resetFields();
            this.dataList = {
                number: record.number,
                unitPrice: record.unitPrice
            }
            this.model = Object.assign({}, record);
            this.visible = true;
            this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model, 'itemCode', 'itemName', 'specifications', 'unit', 'productBatchNumber', 'note', 'attachedItems'))
            })
        },
        close() {
            this.$emit('close');
            this.visible = false;
        },
        handleOk() {
            const that = this;
            // 触发表单验证
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if (!this.model.id) {
                        httpurl += this.url.add;
                        method = 'post';
                    } else {
                        httpurl += this.url.edit;
                        method = 'put';
                    }
                    this.dataList.total = this.total;
                    values = Object.assign(values, this.dataList);
                    let formData = Object.assign(this.model, values);
                    formData['salesBillingId'] = this.mainId
                    httpAction(httpurl, formData, method).then((res) => {
                        if (res.success) {
                            that.$message.success(res.message);
                            that.$emit('ok');
                        } else {
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                        that.close();
                        this.dataList = {};
                    })
                }

            })
        },
        handleCancel() {
            this.dataList = {}
            this.close()
        },
        popupCallback(row) {
            this.form.setFieldsValue(pick(row, 'itemCode', 'itemName', 'specifications', 'unit', 'productBatchNumber', 'note', 'attachedItems'))
        },

    }
}
</script>
